<!doctype html>
<html >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>RSA加密测试</title>
    <script src="https://www.kaka-tech.com/Kaka/Scripts/jquery-2.2.0.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.3.2/jsencrypt.min.js"></script>
    <script type="text/javascript">
        // Call this code when the page is done loading.
        $(function () {
            // Run a quick encryption/decryption when they click.
            $('#testme').click(function () {
                $('#encrypted-output').val('');
                $('#uncrypted-output').val('');
                // 使用公约加密
                var encrypt = new JSEncrypt();
                encrypt.setPublicKey($('#pubkey').val());
                var encrypted = encrypt.encrypt($('#input').val());
                console.log("encrypted: " + encrypted);
                $('#encrypted-output').val(encrypted);
                // 使用私钥解密
                var decrypt = new JSEncrypt();
                decrypt.setPrivateKey($('#privkey').val());
                var uncrypted = decrypt.decrypt(encrypted);
                console.log("uncrypted: " + uncrypted);
                $('#uncrypted-output').val(uncrypted);
                // 原字符串 是否和 解密后字符串 相等
                if (uncrypted == $('#input').val()) {
                    //alert('加密、解密成功！');
                    console.log('加密、解密成功！');
                }
                else {
                    console.log('加密、解密失败');
                    alert('加密、解密失败！');
                }
            });
        });
    </script>
</head>
  <body>
    <div style="color: gray; font-size:xx-small; padding-bottom: 1em; ">移除PEM密钥的备注信息不影响加密、解密（-----BEGIN ... KEY-----、-----END ... KEY-----）</div>
    <label for="pubkey">PEM 格式公约：</label><br/>
    <textarea id="pubkey" rows="6" cols="80">-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76
xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
gwQco1KRMDSmXSMkDwIDAQAB
-----END PUBLIC KEY-----</textarea><br/>
    <label for="privkey">PEM 格式私钥：</label><br/>
    <textarea id="privkey" rows="15" cols="80">-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ
WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR
aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB
AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv
xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH
m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd
8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF
z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5
rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM
V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe
aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil
psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz
uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876
-----END RSA PRIVATE KEY-----</textarea><br/>

    <label for="input">原字符串(加密前)：</label><br/>
    <textarea id="input" name="input" type="text" rows=1 cols=80>这是加密测试字符串，abcd EFGH 1234 !@#$ %^&* _+-=</textarea><br/>

    <label for="encrypted-output">加密后：</label><br/>
    <textarea id="encrypted-output" name="encrypted-output" type="text" rows=3 cols=80></textarea><br/>

    <label for="uncrypted-output">解密后：</label><br/>
    <textarea id="uncrypted-output" name="uncrypted-output" type="text" rows=1 cols=80></textarea><br/>

    <input id="testme" type="button" value="测试" /><br/>
  </body>
</html>